package com.hxg.bl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxg.bl.bean.StoreBean;
import com.hxg.bl.dao.jpa.StoreBeanJpa;
import com.hxg.bl.dao.mapper.StoreMapper;
import com.hxg.bl.service.StoreService;
import com.hxg.bl.vo.SearchVo;
import com.hxg.common.LayuiPageInfo;
import com.hxg.common.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Transactional
@Service("storeService")
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreBeanJpa storeBeanJpa;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private CommonService commonService;

    @Override
    public void saveAndFlushStore(StoreBean storeBean) {
        if (storeBean.getId() == null) {
            storeBean.setOrderNum(storeBean.getOrderNum() - 1);
            storeBeanJpa.saveAndFlush(storeBean);
        }

        String tableName = "cm_store";
        commonService.dealOrderNum(storeBean, storeBeanJpa.getOne(storeBean.getId()), tableName);
        storeBeanJpa.saveAndFlush(storeBean);
        commonService.reSortOrderNum(tableName);
    }

    @Override
    public PageInfo<StoreBean> listStoreByParams(Integer pn, Integer pageSize,
                                                 SearchVo searchVo) {
        PageHelper.startPage(pn, pageSize);

        List<StoreBean> navTabBeanList = storeMapper.listStoreByParams(searchVo);

        return new LayuiPageInfo<>(navTabBeanList);
    }

    @Override
    public void batchDelStore(List<Long> idList) {
        storeBeanJpa.deleteAllByIdIn(idList);
        String tableName = "cm_store";
        commonService.reSortOrderNum(tableName);
    }
}
